package com.hnepri.fang.ftx2.fetch.index;

import com.hnepri.fang.ftx2.model.ResidentialPJ;
import com.hnepri.util.DBUtil;
import com.hnepri.util.FileUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ResidentialIndexWrite {
    static List<ResidentialPJ> parseResidentialIndex(File f) {
        List<ResidentialPJ> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        List<String> lines = FileUtil.getLineList(f.getAbsolutePath(), "utf-8");
        ResidentialPJ residentialPJ = null;
        String tempLine = null;
        try {
            for(String line:lines) {
                tempLine = line;
                String [] datas = line.split("\\|");
                String fid = "", level = "", hydpj = "", bkpj = "", wypj = "", jypj = "";
                fid = datas[0].split(":")[1];
                level = datas[1].split(":")[1];
                if (datas[2].split(":").length == 2) {
                    hydpj = datas[2].split(":")[1];
                }
                if (datas[3].split(":").length == 2) {
                    bkpj = datas[3].split(":")[1];
                }
                if (datas[4].split(":").length == 2) {
                    wypj = datas[4].split(":")[1];
                }
                if (datas[5].split(":").length == 2) {
                    jypj = datas[5].split(":")[1];
                }
                residentialPJ = new ResidentialPJ(fid, level, hydpj, bkpj, wypj, jypj);

                list.add(residentialPJ);
            }
        } catch (Exception e) {
            System.out.println(tempLine);
            e.printStackTrace();
        }

        return list;
    }
    static List<ResidentialPJ> getLines(String city) {
        List<ResidentialPJ> ls = new ArrayList<>();
        String path = "c:/小区/" + city + "/index";
        File f = new File(path);
        File [] fs = f.listFiles();
        for(File tmp:fs) {
            ls.addAll(parseResidentialIndex(tmp));
        }
        return ls;
    }
    static void write2db(String city) {
        Connection con = null;
        PreparedStatement ps = null;
        List<ResidentialPJ> datas = getLines(city);
        try {
            con = DBUtil.getConnection();
            con.setAutoCommit(false);
            String sql = "insert into xiaoqupj(fid,level,hydpj,bkpj,wypj,jypj)" +
                    "values(?,?,?,?,?,?)";
            ps = con.prepareStatement(sql);

            int i = 0;
            for(ResidentialPJ obj:datas) {
                i++;
                ps.setString(1, obj.getFid());
                ps.setString(2, obj.getLevel());
                ps.setString(3, obj.getHydpj());
                ps.setString(4, obj.getBkpj());
                ps.setString(5, obj.getWypj());
                ps.setString(6, obj.getJypj());

                ps.addBatch();
                if (i % 100 == 0) {
                    ps.executeBatch();
                }
            }
            ps.executeBatch();

            con.commit();
        } catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            try {
                DBUtil.closeResource(con, ps);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        write2db("xx");
    }
}
